package com.tyut.controller;

import com.tyut.pojo.Human;
import com.tyut.service.HumanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class HumanController {

    @Autowired
    private HumanService humanService;

    @RequestMapping("/humanAdd")
    public String humanAdd(Model model, Human human) {
        String status = human.getStatus();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        human.setTime(df.format(new Date()));
        try {
            if ("来访".equals(status)) {
                humanService.insertHuman(human);
                return "redirect:humanIn";
            } else {
                humanService.insertHuman(human);
                return "redirect:humanOut";
            }
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("msg", "登记出现错误");
            return "failed";
        }
    }

    @RequestMapping("/humanIn")
    public String humanIn(Model model) {
        List<Human> humanIn = humanService.findHumanIn("来访");
        model.addAttribute("list", humanIn);
        return "humanin";
    }

    @RequestMapping("/humanOut")
    public String humOut(Model model) {
        List<Human> humanOut = humanService.findHumanOut("结束");
        model.addAttribute("list", humanOut);
        return "humanout";
    }

    @RequestMapping("/humanAddJSP")
    public String humanAddJSP(){
        return "humanadd";
    }
}
